<?php


namespace App\Http\Model\Job;


use App\Http\ModelDao\Job\DwDao;
use Illuminate\Support\Facades\DB;

class DwModel
{
    /*监管主体信息*/
    private static $pfs = [];


    /**
     * 数仓-资金监管排课信息-获取需要写入任务队列的数据
     *
     * @param $data
     * @param $offsetId
     * @param $ledgerSubjectId
     *
     * @return array
     */
    public static function pfsHeadAddClassLesson($data,&$offsetId,&$ledgerSubjectId)
    {
        $list = DB::connection($data['db'])
                  ->select('SELECT * FROM `dws_fin_capital_supervision_lesson_list_aks_fd` WHERE `id`>:id AND `ctime`>=:ctime LIMIT 100', [
                      'id'    => $offsetId,
                      'ctime' => $data['data_date'],
                  ]);
        if (empty($list)) {
            return [];
        }
        $arr = [];
        foreach ($list as $val) {
            /*记录主体ID*/
            if ($ledgerSubjectId === null) {
                $ledgerSubjectId = $val->ledger_subject_id;
            }
            /*如果主体ID与第一条数据不一致,则先返回本次结果*/
            if ($ledgerSubjectId !== $val->ledger_subject_id) {
                return $arr;
            }
            $offsetId = $val->id;
            $arr[] = $val;
        }
        return $arr;
    }


    /**
     * 数仓-班级划拨计划表TransferList-获取需要写入任务队列的数据
     *
     * @param $data
     * @param $offsetId
     *
     * @return array
     */
    public static function pfsHeadAddbatchtransfer($data, &$offsetId, &$ledgerSubjectId)
    {
        $list = DB::connection($data['db'])
                  ->select('SELECT * FROM `dwd_fin_capital_supervision_transfer_list_fd` WHERE `id`>:id AND `ctime`>=:ctime LIMIT 100', [
                      'id'    => $offsetId,
                      'ctime' => $data['data_date'],
                  ]);
        if (empty($list)) {
            return [];
        }
        $classId = [];
        $arr = [];
        foreach ($list as $val) {
            /*记录主体ID*/
            if ($ledgerSubjectId === null) {
                $ledgerSubjectId = $val->ledger_subject_id;
            }
            /*如果主体ID与第一条数据不一致,则先返回本次结果*/
            if ($ledgerSubjectId !== $val->ledger_subject_id) {
                return $arr;
            }
            /*如果一个班级存在多次划拨,则拆分批次发起*/
            if (in_array($val->class_id, $classId)) {
                return $arr;
            }
            $classId[] = $val->class_id;
            /*生成数据唯一编码*/
            $val->uniqid = DwDao::uniqid($val->id, $data['db'], $data['table']);
            $offsetId = $val->id;
            $arr[] = $val;
        }
        return $arr;
    }


    /**
     * 数仓-当日推送缴退费汇总表PayClassList（按班级汇总）-计算当天要推送的数据总条数
     *
     * @param $data
     *
     * @return int
     */
    public static function addClassIncrementTotal($data, $ledgerSubjectId)
    {
        $total = DB::connection($data['db'])
                   ->select('SELECT COUNT(1) AS `total` FROM `dws_fin_capital_supervision_pay_class_list_result_fd` WHERE `ledger_subject_id`=:ledger_subject_id AND `ctime`>=:ctime', [
                       'ledger_subject_id' => $ledgerSubjectId,
                       'ctime'             => $data['data_date'],
                   ]);

        return empty($total[0]->total) ? 0 : $total[0]->total;
    }


    /**
     * 数仓-当日推送缴退费汇总表PayClassList（按班级汇总）-获取需要写入任务队列的数据
     *
     * @param $data
     * @param $offsetId
     *
     * @return array
     */
    public static function addClassIncrement($data, &$offsetId, &$ledgerSubjectId, &$createDate)
    {
        $list = DB::connection($data['db'])
                  ->select('SELECT * FROM `dws_fin_capital_supervision_pay_class_list_result_fd` WHERE `id`>:id AND `ctime`>=:ctime LIMIT 100', [
                      'id'    => $offsetId,
                      'ctime' => $data['data_date'],
                  ]);
        if (empty($list)) {
            return [];
        }
        $arr = [];
        foreach ($list as $val) {
            /*记录主体ID*/
            if ($ledgerSubjectId === null) {
                $ledgerSubjectId = $val->ledger_subject_id;
            }
            /*如果主体ID与第一条数据不一致,则先返回本次结果*/
            if ($ledgerSubjectId !== $val->ledger_subject_id) {
                return $arr;
            }
            /*汇总日期*/
            if ($createDate === null) {
                $createDate = $val->create_date;
            }
            /*生成数据唯一编码*/
            $val->uniqid = DwDao::uniqid($val->id, $data['db'], $data['table']);
            $offsetId = $val->id;
            $arr[] = $val;
        }
        return $arr;
    }


    /**
     * 数仓-发生收退费行为的班级列表-获取需要写入任务队列的数据
     *
     * @param $data
     * @param $offsetId
     *
     * @return array
     */
    public static function listClass($data, &$offsetId, &$ledgerSubjectId)
    {
        /*
         * 招行接口: 班级支持批量推送，单个请求推送的班级数最大为500
         * 这里设置LIMIT100,可调整空间<500即可
         * */
        $list = DB::connection($data['db'])
                  ->select('SELECT * FROM `dwd_fin_capital_supervision_class_list_add_fd` WHERE `id`>:id AND `ctime`>=:ctime ORDER BY `id` ASC LIMIT 100', [
                      'id'    => $offsetId,
                      'ctime' => $data['data_date'],
                  ]);
        if (empty($list)) {
            return [];
        }
        $arr = [];
        foreach ($list as $val) {
            /*记录主体ID*/
            if ($ledgerSubjectId === null) {
                $ledgerSubjectId = $val->ledger_subject_id;
            }
            /*如果主体ID与第一条数据不一致,则先返回本次结果*/
            if ($ledgerSubjectId !== $val->ledger_subject_id) {
                return $arr;
            }
            /*生成数据唯一编码*/
            $val->uniqid = DwDao::uniqid($val->id, $data['db'], $data['table']);
            $offsetId = $val->id;
            $arr[] = $val;
        }
        return $arr;
    }


    /**
     * 查询指定主体监管账户信息
     *
     * @param $ledgerSubjectId
     *
     * @return array|mixed
     */
    public static function pfs($ledgerSubjectId)
    {
        if (empty($ledgerSubjectId)) {
            return [];
        }
        /*如果监管主体已就查询过了,且没有失效,则直接返回*/
        if (!empty(self::$pfs[$ledgerSubjectId]) && self::$pfs[$ledgerSubjectId]['ttl'] > time()) {
            return self::$pfs[$ledgerSubjectId]['val'];
        }
        $data = DB::select('SELECT * FROM `pfs_dw_ini` WHERE `ledger_subject_id`=:ledger_subject_id AND `is_del`=0 LIMIT 1', [
            'ledger_subject_id' => $ledgerSubjectId,
        ]);
        self::$pfs[$ledgerSubjectId] = [
            /*设置失效时间为10秒*/
            'ttl' => time() + 10,
            'val' => empty($data) ? [] : current($data),
        ];
        return self::$pfs[$ledgerSubjectId]['val'];
    }


}





/**
 * DwModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/4 21:29:00   操作:创建
 **/
